﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;

public partial class Admin_UserControl_ucThemSP : System.Web.UI.UserControl
{
    TblItemsDAO ItemsDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    TblItemImageDAO imageDAO = new TblItemImageDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            hypDetails.NavigateUrl = "~/Admin/WebContent.aspx?idItem=" + id;
            if (id == null || id == "")
            {
                fileUpload1.Visible = false;
                lblImage.Visible = false;
                // a.Visible = false;
                //b.Visible = false;
                //c.Visible = false;
                lbltitle.Text = "Thêm Sản Phẩm";
                LoadCategories();
            }
            else
            {
                lbltitle.Text = "Cập nhật sản phẩm";
                LoadCategories();
                LoadItemById();
                LoadImgItemByID();
            }
        }
    }
    private void LoadImgItemByID()
    {
        string id = Request.QueryString["id"];
        grdImgItems.DataSource = ItemsDAO.GetImgItems(Convert.ToInt32(id));
        grdImgItems.DataBind();
    }

    private void LoadItemById()
    {
        LoadCategories();
        string id = Request.QueryString["id"];
        TblItems Item = ItemsDAO.GetItemByID(Convert.ToInt32(id));
        txtItemName.Text = Item.ItemName;
        txtNumber.Text = Item.ItemNumber;
        drCategory.SelectedValue = Item.CateID.ToString();
        txtDes.Content = Server.HtmlDecode(Item.ItemDescription);
        txtPrice.Text = Item.BaseSellingPrice.ToString();
        drInactive.SelectedValue = Item.IsInactive;
        if (Item.ItemType == 0)
        {
            drType.SelectedItem.Text = "Sản phẩm bán chạy";
        }
        if (Item.ItemType == 1)
        {
            drType.SelectedItem.Text = "Sản Phẩm Hot";
        }
        if (Item.ItemType == 2)
        {
            drType.SelectedItem.Text = "Sản phẩm khuyến mại";
        }
    }

    private void LoadCategories()
    {
        drCategory.DataSource = ItemsDAO.GetCategory();
        drCategory.DataTextField = "CateName";
        drCategory.DataValueField = "CateID";
        drCategory.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool flag = false;
        string id = Request.QueryString["id"];
        try
        {
            TblItems Item = new TblItems();
            Item.ItemName = txtItemName.Text;
            Item.ItemNumber = txtNumber.Text;
            if (drInactive.SelectedValue == "0")
                Item.IsInactive = "N";
            else
                Item.IsInactive = "Y";
            Item.QuantityOnHand = 0;
            Item.ValueOnHand = 0;
            Item.ItemDescription = Server.HtmlEncode(txtDes.Content);
            Item.CateID = Convert.ToInt32(drCategory.SelectedValue);
            Item.BaseSellingPrice = Convert.ToDouble(txtPrice.Text);
            Item.PriceIsInclusive = "N";
            Item.SellTaxCodeID = 2;
            Item.ItemType = Convert.ToInt32(drType.SelectedValue);


            if (id == null || id == "")
            {
                Item.ItemID = 0;
                ItemsDAO.InsUpdItem(Item, 1);
            }
            else
            {
                Item.ItemID = Convert.ToInt32(id);
                ItemsDAO.InsUpdItem(Item, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            if (id == null || id == "")
                Response.Redirect("~/Admin/DanhSachSanPham.aspx");
            else
                //Response.Redirect("~/Admin/AddItem.aspx?id=" + id);
                Response.Redirect("~/Admin/DanhSachSanPham.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {

            string path = Server.MapPath("~\\Admin\\images\\Items\\");
            string filename = id + "_" + fileUpload1.FileName;

            TblItemImage imgItem = new TblItemImage();
            // lblMessage.Text = path;
            if (File.Exists(path + "\\" + filename))
            {
                lblMessage.Text = "The image file already exist. Please choose other file or change the file name to upload";
                return;
            }
            fileUpload1.SaveAs(path + "\\" + filename);
            imgItem.ImageURL = filename;
            imgItem.ItemID = Convert.ToInt32(id);

            ItemsDAO.InsImageItem(imgItem, 1);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            if (id == null || id == "")
                Response.Redirect("~/Admin/ThemSanPham.aspx?id=" + id);
            else
                Response.Redirect("~/Admin/ThemSanPham.aspx?id=" + id);
        }
    }

    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblItemImage imgItem = new TblItemImage();

            imgItem.ID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            imgItem = imageDAO.GetItemImageByID(Global.ConvertToInt(id), idXoa);
            ItemsDAO.DeleteImgItem(imgItem);

            string filename = imgItem.ImageURL;
            if (File.Exists(Server.MapPath("~\\Admin\\images\\Items\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\images\\Items\\" + filename));
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadImgItemByID();
        }

    }
}
